package com.hmall.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 短信发送工具类
 */
@Data
@AllArgsConstructor
public class SMSUtils {

    private String regionId;

    private String accessKeyId;

    private String accessKeySecret;

    private static final Logger log = LoggerFactory.getLogger(SMSUtils.class);

    /**
     * 发送短信
     *
     * @param signName     签名
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param        参数
     */
    public void sendMessage(String signName, String templateCode, String phoneNumbers, String param) {
        log.info("短信发送开始");
        log.info("======regionId: " + regionId);
        log.info("======accessKeyId: " + accessKeyId);
        log.info("======accessKeySecret: " + accessKeySecret);
        // 初始化ascClient需要的几个参数
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":\"" + param + "\"}");
        try {
            client.getAcsResponse(request);
            log.info("短信发送成功");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}